package com.hongbo.echarts;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import com.alibaba.fastjson.JSON;
import com.github.abel533.echarts.Option;
import com.github.abel533.echarts.axis.CategoryAxis;
import com.github.abel533.echarts.axis.ValueAxis;
import com.github.abel533.echarts.code.Trigger;
import com.github.abel533.echarts.data.PieData;
import com.github.abel533.echarts.json.GsonUtil;
import com.github.abel533.echarts.series.Bar;
import com.github.abel533.echarts.series.Pie;
import com.jfinal.core.Controller;

public class EchartsController extends Controller{
	public void index() {
//		renderText("Hello JFinal World.  IndexController");
		
		String optionStr  = null;
		//GsonUtil方法中的format带来了java.lang.reflect.InvocationTargetException
		//可以剔除pom里面关于gson的引用
		//由于Gson大量使用reflect,GsonUtil没有加以判断，所以此处改用fastjson库
//		optionStr = GsonUtil.format(selectRemoveCauses());
		optionStr = JSON.toJSONString(selectRemoveCauses());
		setAttr("option",optionStr);


		optionStr = JSON.toJSONString(selectSelling());
		setAttr("saleData", optionStr);
		render("index.html");
//		render("echart1.html");

	}

	public Option selectRemoveCauses() {

		// 查询前20
		// 数据库查询获取统计数据
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();

		for (int i = 0; i < 10; i++) {

			Map<String, Object> map = new HashMap<String, Object>();
			map.put("NAME", "张三" + i);
			map.put("TOTAL", new Random().nextInt(50));
			list.add(map);

		}

		// 创建Option
		Option option = new Option();
		option.title("剔除药品").tooltip(Trigger.axis).legend("金额（元）");

		// 横轴为值轴
		option.xAxis(new ValueAxis().boundaryGap(0d, 0.01));

		// 创建类目轴
		CategoryAxis category = new CategoryAxis();

		// 柱状数据
		Bar bar = new Bar("金额（元）");

		// 饼图数据
		Pie pie = new Pie("金额（元）");

		// 循环数据
		for (Map<String, Object> objectMap : list) {

			// 设置类目
			category.data(objectMap.get("NAME"));
			System.out.println(objectMap.get("NAME"));

			// 类目对应的柱状图
			bar.data(objectMap.get("TOTAL"));

			// 饼图数据
			System.out.println(objectMap.get("TOTAL"));
			pie.data(new PieData(objectMap.get("NAME").toString(), objectMap.get("TOTAL")));

		}

		// 设置类目轴
		option.yAxis(category);

		// 饼图的圆心和半径
		pie.center(900, 380).radius(100);

		// 设置数据
		option.series(bar, pie);

		// 由于药品名字过长，图表距离左侧距离设置180，关于grid可以看ECharts的官方文档
		option.grid().x(180);

		// 返回Option
		return option;

	}

	
	public Option selectSelling() {

		// 查询前20
		// 数据库查询获取统计数据
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		String[] str = new String[]{"衬衫","羊毛衫","雪纺衫","裤子","高跟鞋","袜子","皮鞋","七分裤"};
//		String[] str = new String[]{"1","2","3","4","5","6","7","8"};

		for (int i = 0; i < str.length; i++) {

			Map<String, Object> map = new HashMap<String, Object>();
			map.put("NAME", str[i]);
			map.put("TOTAL", new Random().nextInt(100));
			list.add(map);

		}

		// 创建Option
		Option option = new Option();
		List<String> legendList = new ArrayList<String>();
		legendList.add("销量");
//		option.title("剔除药品").tooltip(Trigger.axis).legend("金额（元）");
		option.title("ECharts 测试").tooltip(Trigger.axis).legend().setData(legendList);;


		// 创建类目轴
		CategoryAxis category = new CategoryAxis();

		// 柱状数据
//		Bar bar = new Bar("销量");
		Bar bar = new Bar(legendList.get(0));

		// 饼图数据
//		Pie pie = new Pie("销量");
//		Pie pie = new Pie(legendList.get(0));

		// 循环数据
		for (Map<String, Object> objectMap : list) {

			// 设置类目
			category.data(objectMap.get("NAME"));
			System.out.println(objectMap.get("NAME"));

			// 类目对应的柱状图
			bar.data(objectMap.get("TOTAL"));

			// 饼图数据
			System.out.println(objectMap.get("TOTAL"));
//			pie.data(new PieData(objectMap.get("NAME").toString(), objectMap.get("TOTAL")));

		}
		
//		设置横轴为值轴
		option.xAxis(category);
//		设置纵轴为类目轴
		option.yAxis(new ValueAxis().boundaryGap(0d, 0.01));
		
		//设置横为值轴
//		option.xAxis(new ValueAxis().boundaryGap(0d, 0.01));
//		// 纵轴为类目
//		option.yAxis(category);


		// 饼图的圆心和半径
//		pie.center(900, 380).radius(100);

		// 设置数据
//		option.series(bar, pie);
		option.series(bar);

		// 由于药品名字过长，图表距离左侧距离设置180，关于grid可以看ECharts的官方文档
		option.grid().x(40);

		// 返回Option
		return option;

	}
}
